package cn.wit.summit.chwan.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.response.ImageListByTypeResponse;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.widgets.photoviewer.HackyViewPager;
import cn.wit.summit.chwan.widgets.photoviewer.ImageDetailFragment;
import retrofit2.Retrofit;

/**
 * Created by hezl on 2015/11/23.
 */
public class ImagePagerV2Activity extends FragmentActivity {
    @Bind(R.id.pager)
    HackyViewPager mPager;
    @Bind(R.id.bottomLayout)
    TextView bottomLayout;
    @Bind(R.id.topLayout)
    TextView topLayout;

    private List<String> urls = new ArrayList<>();
    private static final String STATE_POSITION = "STATE_POSITION";
    public static String EXTRA_IMAGE_DATA = "data";
    public static String EXTRA_IMAGE_INDEX = "index";
    public static String EXTRA_PN = "pn";
    public static String EXTRA_PC = "pc";
    public static String EXTRA_ID = "id";
    public static String EXTRA_GAMEID = "gameid";
    public static String EXTRA_TOTAL = "totalPic";

    private String totalPic;
    private String typeId;
    private String gameId;
    private int pagerPosition, pn, pc;
    private List<ImageListByTypeResponse.PicListBean> readData = new ArrayList<>();

    private RetrofitManager retrofitManager;
    private Retrofit retrofit;
    private ApiUtil api;

    private ImagePagerAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        setContentView(R.layout.activity_image_viewpager_v2);
        ButterKnife.bind(this);

        retrofitManager = RetrofitManager.getInstance(this);
        retrofit = retrofitManager.getRetrofit();
        api = retrofit.create(ApiUtil.class);

        readData = (List<ImageListByTypeResponse.PicListBean>) getIntent().getSerializableExtra(EXTRA_IMAGE_DATA);//获取list方
        pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
        pn = getIntent().getIntExtra(EXTRA_PN, 1);
        pc = getIntent().getIntExtra(EXTRA_PC, 10);
        typeId = getIntent().getStringExtra(EXTRA_ID);
        gameId = getIntent().getStringExtra(EXTRA_GAMEID);
        totalPic = getIntent().getStringExtra(EXTRA_TOTAL);

        if (readData == null || readData.size() == 0) {
            finish();
            return;
        }
        urls.clear();
        for (int i = 0; i < readData.size(); i++) {
            if (i == 0) {
                bottomLayout.setText(readData.get(i).getDescribe());
            }
            urls.add(readData.get(i).getPicPath());
//            urls.add("http://static-img.abab618.com/uploads/yiqiwan/images/fa/fa/fafa5efeaf3cbe3b23b2748d13e629a1.jpg");
        }

        topLayout.setText((pagerPosition+1)+"/"+totalPic);
        mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls);
        mPager.setAdapter(mAdapter);

        // 更新下标
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int arg0) {
                if (arg0>=readData.size())
                    arg0 = readData.size() - 1;
                bottomLayout.setText(readData.get(arg0).getDescribe());
                if (urls.size() >= pc && arg0 >= urls.size() - 4) {
                    getWebData();
                }
                topLayout.setText((arg0+1)+"/"+totalPic);
            }

        });
        if (savedInstanceState != null) {
            pagerPosition = savedInstanceState.getInt(STATE_POSITION);
        }

        mPager.setCurrentItem(pagerPosition);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_POSITION, mPager.getCurrentItem());
    }

    @OnClick({R.id.topBackLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.topBackLayout:
                finish();
                break;
        }
    }

    private class ImagePagerAdapter extends FragmentStatePagerAdapter {

        public List<String> fileList;

        public ImagePagerAdapter(FragmentManager fm, List<String> fileList) {
            super(fm);
            this.fileList = fileList;
        }

        @Override
        public int getCount() {
            return fileList == null ? 0 : fileList.size();
        }

        @Override
        public Fragment getItem(int position) {
            String url = fileList.get(position);
            return ImageDetailFragment.newInstance(url);
        }

    }

    private void getWebData() {
        api.getPicList(gameId, typeId, pn, pc).enqueue(new MyCallback<ResponseBase<ImageListByTypeResponse>>() {
            @Override
            public void onSuccess(ResponseBase<ImageListByTypeResponse> model) {
                ImageListByTypeResponse data = model.getData_info();
                if (data != null || data.getPicList() != null || data.getPicList().size() > 0) {
                    for (int i = 0; i < data.getPicList().size(); i++) {
                        urls.add(readData.get(i).getPicPath());
                    }
                    mAdapter.notifyDataSetChanged();
                    pn += 1;
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }
}